// 获取所有学员的成绩
function getScore() {
    axios.get('/score/list').then(res => {

        const { code, message, data } = res.data
        let trs = ''
        for (let k in data) {
            trs += `<tr>
        <th scope="row">${k}</th>
        <td>${data[k].name}</td>
        <td class="score" data-id=${k} data-batch='1'>${data[k].score[0]}</td>
        <td class="score" data-id=${k} data-batch='2'>${data[k].score[1]}</td>
        <td class="score" data-id=${k} data-batch='3'>${data[k].score[2]}</td>
        <td class="score" data-id=${k} data-batch='4'>${data[k].score[3]}</td>
        <td class="score" data-id=${k} data-batch='5'>${data[k].score[4]}</td>
      </tr>`
        }
        document.querySelector('tbody').innerHTML = trs
    })
}
getScore()
document.querySelector('tbody').addEventListener('click', function (e) {
    if (e.target.tagName === 'INPUT') return
    if (e.target.tagName === 'TD' && e.target.className === 'score') {
        //获取td
        const td = e.target
        const id = td.dataset.id
        const batch = td.dataset.batch
        //获取td的值
        const tdValue = td.innerHTML
        //创建input输入框
        td.innerHTML = '<input type="text"/>'
        td.children[0].value = tdValue
        //获得焦点
        td.children[0].focus()
        td.children[0].addEventListener('blur', function () {
            td.innerHTML = tdValue
        })
        //键盘弹起
        td.children[0].addEventListener('keyup', function (e) {
            if (e.key === 'Enter') {
                const data = {
                    stu_id: id,
                    batch,
                    score: this.value
                }
                // console.log(data)
                axios.post('/score/entry',data).then(res=>{
                    // console.log(res)
                    const {code,message}=res.data
                    if (code===0) {
                        //重新渲染
                        getScore()
                    }
                })
            }
        })
    }
})
